package com.hps.demo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hps.demo.annotation.RpcService;
import com.hps.demo.entity.InfoUser;
import com.hps.demo.service.InfoUserService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import lombok.extern.slf4j.Slf4j;

/**
 * @author heps
 * @since 2020/8/15
 */
@Slf4j
@RpcService
public class InfoUserImplServiceImpl implements InfoUserService {

  Map<String, InfoUser> infoUserMap = new HashMap<>();

  @Override
  public List<InfoUser> insertInfoUser(InfoUser infoUser) {
    log.info("新增用户信息:{}", JSONObject.toJSONString(infoUser));
    infoUserMap.put(infoUser.getId(),infoUser);
    return getInfoUserList();
  }
  @Override
  public InfoUser getInfoUserById(String id) {
    InfoUser infoUser = new InfoUser();
    infoUser.setId(id);
    infoUser.setName("user:" + id);
    log.info("查询用户ID:{}",id);
    return infoUser;
  }

  public List<InfoUser> getInfoUserList() {
    List<InfoUser> userList = new ArrayList<>();
    Iterator<Entry<String, InfoUser>> iterator = infoUserMap.entrySet().iterator();
    while (iterator.hasNext()){
      Entry<String, InfoUser> next = iterator.next();
      userList.add(next.getValue());
    }
    log.info("返回用户信息记录数:{}",userList.size());
    return userList;
  }

  @Override
  public void deleteInfoUserById(String id) {
    log.info("删除用户信息:{}", JSONObject.toJSONString(infoUserMap.remove(id)));
  }

  @Override
  public String getNameById(String id){
    log.info("根据ID查询用户名称:{}",id);
    return infoUserMap.get(id).getName();
  }

  @Override
  public Map<String,InfoUser> getAllUser() {
    log.info("查询所有用户信息{}",infoUserMap.keySet().size());
    return infoUserMap;
  }
}
